var EventAddOrUpdate = function () {
	
    var handleImages = function() {

        // see http://www.plupload.com/
        var uploader = new plupload.Uploader({
            runtimes : 'html5,flash,silverlight,html4',
             
            browse_button : document.getElementById('tab_images_uploader_pickfiles'), // you can pass in id...
            container: document.getElementById('tab_images_uploader_container'), // ... or DOM Element itself
             
            url : "/kk/admin/photo.json",
             
            filters : {
                max_file_size : '5mb',
                mime_types: [
                    {title : "Image files", extensions : "jpg,gif,png"},
                    {title : "Zip files", extensions : "zip"}
                ]
            },
         
            // Flash settings
            flash_swf_url : 'admin/assets/plugins/plupload/js/Moxie.swf',
     
            // Silverlight settings
            silverlight_xap_url : 'admin/assets/plugins/plupload/js/Moxie.xap',             
         
            init: {
                PostInit: function() {
                    $('#tab_images_uploader_filelist').html("");
         
                    $('#tab_images_uploader_uploadfiles').click(function() {
                        uploader.start();
                        return false;
                    });

                    $('#tab_images_uploader_filelist').on('click', '.added-files .remove', function(){
                        uploader.removeFile($(this).parent('.added-files').attr("id"));    
                        $(this).parent('.added-files').remove();                     
                    });
                },
         
                FilesAdded: function(up, files) {
                    plupload.each(files, function(file) {
                        $('#tab_images_uploader_filelist').append('<div class="alert alert-warning added-files" id="uploaded_file_' + file.id + '">' + file.name + '(' + plupload.formatSize(file.size) + ') <span class="status label label-info"></span>&nbsp;<a href="javascript:;" style="margin-top:-5px" class="remove pull-right btn btn-sm red"><i class="fa fa-times"></i> remove</a></div>');
                    });
                },
         
                UploadProgress: function(up, file) {
                    $('#uploaded_file_' + file.id + ' > .status').html(file.percent + '%');
                },

                FileUploaded: function(up, file, response) {
                    var response = $.parseJSON(response.response);
                    if (response.result && response.result == 'OK') {
                        var id = response.id; // uploaded file's unique name. Here you can collect uploaded file names and submit an jax request to your server side script to process the uploaded files and update the images tabke
                        $('#uploaded_file_' + file.id + ' > .status').removeClass("label-info").addClass("label-success").html('<i class="fa fa-check"></i> Done'); // set successfull upload
                    var html='<tr><td>'
                    	+'	<a href="'+response.src+'" class="fancybox-button" data-rel="fancybox-button">'
						+'	<img class="img-responsive" src="'+response.src+'" alt="">'
						+'	</a>'
						+'</td></tr>';
						 
                    $("#pic").val(response.picPath);
                     $("#event_pic").html(html);
                        
                    } else {
                        $('#uploaded_file_' + file.id + ' > .status').removeClass("label-info").addClass("label-danger").html('<i class="fa fa-warning"></i> Failed'); // set failed upload
                        Metronic.alert({type: 'danger', message: 'One of uploads failed. Please retry.', closeInSeconds: 10, icon: 'warning'});
                    }
                },
         
                Error: function(up, err) {
                    Metronic.alert({type: 'danger', message: err.message, closeInSeconds: 10, icon: 'warning'});
                }
            }
        });

        uploader.init();

    }
    var add=function(){

    	
    	  $("#price").inputmask({
              "mask": "9",
              "repeat": 10,
              "greedy": false
          });
    	  $("#amount").inputmask({
              "mask": "9",
              "repeat": 10,
              "greedy": false
          });

    	$(".save").click(function(){
    		
	    		$( "#eventForm" ).validate({
	  			  rules: {
	  			    field: {
	  			      required: true
	  			    }
	  			  },
	              success: function(label) { 
	            	
	              }, 
	  			});
	  		if($("#pic").val().length >0){
		  		var data =$("#eventForm").serializeForm();
		  		data.description =UE.getEditor('editor').getContent();	
	  		  $.ajax({
	    			contentType: "application/json;charset=UTF-8",
	     			data:$("#eventForm").serializeForm(),
	    			data: $.toJSON($("#eventForm").serializeForm()),
	    			url:"admin/user/tologin.json",
	    			dataType : 'json',
	    			type:'POST',
	    			success : function(data) {
	    				console.log(data);
	    				if(data && data.result){
	    					  Metronic.alert({type: 'success', message: '添加成功', closeInSeconds: 10, icon: 'success'}); 
	    				}
	    			}
	    		});
	  		
	  		}else{
	  			 Metronic.alert({type: 'danger', message: "请上传照片", closeInSeconds: 10, icon: 'warning'});
	  		}
    		
    	});
    	
    	$(".saveandnew").click(function(){
    		
    		$( "#eventForm" ).validate({
  			  rules: {
  			    field: {
  			      required: true
  			    }
  			  },
              success: function(label) { 
            	
              }, 
  			});
	  		if($("#pic").val().length >0){
		  		var data =$("#eventForm").serializeForm();
		  		data.description =UE.getEditor('editor').getContent();
	  		  $.ajax({
	    			contentType: "application/json;charset=UTF-8",
	     			data:$("#eventForm").serializeForm(),
	    			data: $.toJSON($("#eventForm").serializeForm()),
//	    			url:"admin/event/addOrUpdate.json",
	    			url:"admin/event/addOrUpdate.json",
	    			dataType : 'json',
	    			type:'POST',
	    			success : function(data) {
	    				console.log(data);
	    				if(data && data.result){
	    					  Metronic.alert({type: 'success', message: '添加成功', closeInSeconds: 10, icon: 'success'});
	    					  windows.location.reload();
	    				}else{
	    					 Metronic.alert({type: 'danger', message: data.msg, closeInSeconds: 10, icon: 'warning'});
	    				}
	    			}
	    		});
	  		
	  		}else{
	  			 Metronic.alert({type: 'danger', message: "请上传照片", closeInSeconds: 10, icon: 'warning'});
	  		}
		
    	});
    	
    	$(".editSave").click(function(){
    		$( "#eventForm" ).validate({
  			  rules: {
  			    field: {
  			      required: true
  			    }
  			  },
              success: function(label) { 
            	
              }, 
  			});
  		if($("#pic").val().length >0){
	  		var data =$("#eventForm").serializeForm();
	  		data.description =UE.getEditor('editor').getContent();
  		  $.ajax({
  			  
  			  
    			contentType: "application/json;charset=UTF-8",
    			data: $.toJSON(data),
    			url:"admin/event/addOrUpdate.json",
    			dataType : 'json',
    			type:'POST',
    			success : function(data) {
    				console.log(data);
    				if(data && data.result){
    					  Metronic.alert({type: 'success', message: '修改成功', closeInSeconds: 10, icon: 'success'}); 
    				}else{
    					 Metronic.alert({type: 'danger', message: data.msg, closeInSeconds: 10, icon: 'warning'});
    				}
    			}
    		});
  		
  		}else{
  			 Metronic.alert({type: 'danger', message: "请上传照片", closeInSeconds: 10, icon: 'warning'});
  		}
		
	});
    	
    	
    }

    var initComponents = function () {
    
        //init datepickers
        $('.date-picker').datepicker({
            rtl: Metronic.isRTL(),
            autoclose: true
        });

        //init datetimepickers
        $(".datetime-picker").datetimepicker({
            isRTL: Metronic.isRTL(),
            autoclose: true,
            todayBtn: true,
            pickerPosition: (Metronic.isRTL() ? "bottom-right" : "bottom-left"),
            minuteStep: 10
        });

        //init maxlength handler
        $('.maxlength-handler').maxlength({
            limitReachedClass: "label label-danger",
            alwaysShow: true,
            threshold: 5
        });
        
            $(".form_datetime").datetimepicker({
                autoclose: true,
                isRTL: Metronic.isRTL(),
                format: "yyyy-mm-dd   hh:ii",
                pickerPosition: (Metronic.isRTL() ? "bottom-right" : "bottom-left")
            });

            $('body').removeClass("modal-open"); // fix bug when inline picker is used in modal

        
       
    }

    return {

        //main function to initiate the module
        init: function () {
            initComponents();

            handleImages();

            add();
        }

    };

}();
var test={};
